<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input
        clearable
        placeholder="请输入车牌号码"
        class="search-main"
        v-model="params.carNumber"
      />
      <span class="search-label">车主姓名：</span>
      <el-input
        clearable
        placeholder="请输入车主姓名"
        class="search-main"
        v-model="params.personName"
        autocomplete
      />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <!-- lable显示的就是select显示的 -->
        <!-- value是传递给select的value -->
        <el-option
          v-for="item in statusList"
          :key="item.value"
          :value="item.value"
          :label="item.text"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="getCardList(1)"
        >查询</el-button
      >
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="addMonthCard">添加月卡</el-button>
      <el-button @click="batchDeleteCar">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="rows"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index" label="序号" :index="indexMethod()" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column
          label="状态"
          prop="cardStatus"
          :formatter="formatStatus"
        />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <!-- 通过插槽来拿到id值，这里插槽是scope -->
            <!-- row就相当于item，就是当前的item -->
            <!-- 所用域插槽可以传递数据，这里传递了id值 -->
            <el-button size="mini" type="text" @click="editCard(scope.row.id)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="deleteCard(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <!-- :current-page当前页码 -->
      <!-- :page-sizes 可选择的每页页容量 -->
      <!-- :page-size 当前的页容量 -->
      <!-- size-change 页容量变化的触发事件 -->
      <!-- current-change 当前页码变化的触发事件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[2, 10, 50, 100]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin: 20px auto; text-align: center"
      >
      </el-pagination>
    </div>
    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
        <!-- <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form> -->
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetCardListAPI, reqDelCardAPI } from "../../../api/card";
export default {
  name: "Card",
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        carNumber: "",
        personName: "",
        cardStatus: null,
      },
      // 接口返回结果
      rows: [],
      total: 0,
      statusList: [
        {
          text: "全部",
          value: null,
        },
        {
          text: "可用",
          value: "0",
        },
        {
          text: "已过期",
          value: "1",
        },
      ],
      selectionValue: [],
    };
  },
  created() {
    this.getCardList();
  },
  methods: {
    // 获取列表数据
    async getCardList(page = null) {
      if (page) {
        this.params.page = page;
      }
      const res = await reqGetCardListAPI(this.params);
      console.log(res);
      this.rows = res.data.rows;
      this.total = res.data.total;
    },

    formatStatus(row, column, cellvalue, index) {
      // if(cellvalue == 0){
      //   return "正常";
      // }else{
      //   return "已过期";
      // }
      const MAP = {
        0: "正常",
        1: "已过期",
      };
      return MAP[cellvalue];
    },
    handleCurrentChange(e) {
      console.log(e, "我是current");
      this.params.page = e;
      this.getCardList();
    },
    handleSizeChange(e) {
      console.log(e, "我是size");
      this.params.pageSize = e;
      this.getCardList();
    },
    // 索引规范
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pageSize + 1;
    },

    // 添加月卡
    addMonthCard() {
      this.$router.push({ path: "/car/addMonth" });
    },
    // 修改月卡
    editCard(id) {
      this.$router.push({
        path: "/car/addMonth",
        // 传递id，子组件接收
        query: { id },
      });
    },
    // 删除月卡
    deleteCard(id) {
      this.$confirm("此操作将永久删除该条目, 是否继续?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await reqDelCardAPI(id);
          if (res.code == 50000) {
            this.$message.error(res.msg);
            return;
          }
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 判定当前删除的数据是不是最一项
          if (this.rows.length == 1 && this.params.page > 1) {
            this.params.page--;
          }
          this.getCardList();
        })
        // catch最好写上，否则会报错
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 获取选中的所有数据
    handleSelectionChange(val) {
      console.log(val);
      // 现在数组中是一个一个对象，需要包含id的一个数组
      this.selectionValue = val;
    },
    // 删除选中
    batchDeleteCar() {
      if (this.selectionValue.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择要删除的记录！",
        });
        return;
      }
      // 修改当前数组，使用map方法，获取只有id的数组
      const newArr = this.selectionValue.map((item) => item.id);
      this.$confirm("您确定要删除吗？", "温馨提示")
        .then(async () => {
          const res = await reqDelCardAPI(newArr.join(","));
          if (res.code == 50000) {
            this.$message.error(res.msg);
            return;
          }
          this.$notify.success("删除成功");
          this.params.page = 1;
          this.params.pageSize = 10;
          this.getCardList();
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn {
    margin-left: 20px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
